//
//  CYUIView_StarLevel.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/18.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "CYUIView_StarLevel.h"
#define kStarCount 5
@interface CYUIView_StarLevel()
{
    BOOL _isAddStar;
    
}
@property (strong, nonatomic)UIImageView *starOne;
@property (strong, nonatomic)UIImageView *starTwo;
@property (strong, nonatomic)UIImageView *starThree;
@property (strong, nonatomic)UIImageView *starFour;
@property (strong, nonatomic)UIImageView *starFive;
@end
@implementation CYUIView_StarLevel
- (void)addSubviews
{
    [super addSubviews];
    _imageWidth = 20;
    _hPadding = 5;
    _supportFloat = NO;
    _minScore = 0;
    _defaultScore = 0;
    for (NSInteger i = 0; i < kStarCount ; i++) {
        [self makeStarViewWithImageName:self.normalImageName startPosition:i];
    }
    
}
- (UIImageView *)starOne
{
    if (!_starOne) {
        _starOne = [self createImageView:0];
    }
    return _starOne;
}
- (UIImageView *)starTwo
{
    if (!_starTwo) {
        _starTwo = [self createImageView:1];
    }
    return _starTwo;
}
- (UIImageView *)starThree
{
    if (!_starThree) {
        _starThree = [self createImageView:2];
    }
    return _starThree;
}
- (UIImageView *)starFour
{
    if (!_starFour) {
        _starFour = [self createImageView:3];
    }
    return _starFour;
}
- (UIImageView *)starFive
{
    if (!_starFive) {
        _starFive = [self createImageView:4];
    }
    return _starFive;
}
#pragma mark - 根据图片创建星星
- (void)makeStarViewWithImageName:(NSString *)imageName startPosition:(NSInteger) position{
    
    //如果重新设置评分,会重新创建imageView 需要判断 若已经存在5个imageView不去创建
    NSArray *imageArr = @[self.starOne,self.starTwo,self.starThree,self.starFour,self.starFive];
    UIImageView *imageView = [imageArr objectAtIndex:position];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.width = self.imageWidth;
    imageView.height = self.imageWidth;
    imageView.frame = CGRectOffset(imageView.bounds, (imageView.bounds.size.width + _hPadding)*position,self.frame.size.height/2 - self.imageWidth/2);
    
}
-(UIImageView *)createImageView:(NSInteger)index{
    UIImageView * imageView = [[UIImageView alloc]init];
    [imageView sizeToFit];
    imageView.width = _imageWidth;
    imageView.height = _imageWidth;
    imageView.frame = CGRectOffset(imageView.bounds, imageView.bounds.size.width*index, 0);
    [self addSubview:imageView];
    return imageView;
}
- (void)setDefaultScore:(CGFloat)defaultScore
{
    _defaultScore = MIN(kStarCount, defaultScore);
    self.score = _defaultScore;
    NSArray *imageArr = @[self.starOne,self.starTwo,self.starThree,self.starFour,self.starFive];
    for (int i = 0; i< kStarCount; i++) {
        UIImageView *imageView = [imageArr objectAtIndex:i];
        if (i<defaultScore) {
            imageView.image = [UIImage imageNamed:self.selectedImageName];
            [self makeStarViewWithImageName:self.selectedImageName startPosition:i];

        } else {
            [self makeStarViewWithImageName:self.normalImageName startPosition:i];
        }
    }

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if((point.x > self.starOne.x && point.x < (self.starFive.x + self.starFive.width))&&(point.y > self.starOne.y && point.y< self.height)){
        _isAddStar = YES;
        
    }else{
        _isAddStar = NO;
    }
}
- (void)setNormalImageName:(NSString *)normalImageName
{
    _normalImageName = normalImageName;
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if(_isAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        [self setStarForegroundViewWithPoint:point];
        
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(_isAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        [self setStarForegroundViewWithPoint:point];
    }
    _isAddStar = NO;
}
-(void)setStarForegroundViewWithPoint:(CGPoint)point{
    
    self.score = 0;
    if (self.minScore > 0) {
            self.score = self.minScore;
            [self.starOne setImage:[UIImage imageNamed:self.selectedImageName]];
    } else {
        self.score = self.score + [self changeImg:point.x image:self.starOne];
        
    }

    self.score = self.score + [self changeImg:point.x image:self.starTwo];
    self.score = self.score + [self changeImg:point.x image:self.starThree];
    self.score = self.score + [self changeImg:point.x image:self.starFour];
    self.score = self.score + [self changeImg:point.x image:self.starFive];
    //评论最少半星
    if(self.score == 0 && self.supportFloat){
        self.score = 0.5;
        [self.starOne setImage:[UIImage imageNamed:self.halfImageName]];
    }
    
    DLog(@"分数 %f",self.score);
    if (self.block_score) {
        self.block_score(@(self.score));
    }
}


-(CGFloat)changeImg:(float)x image:(UIImageView*)img{
    if(x > img.x + img.width/2){
        [img setImage:[UIImage imageNamed:self.selectedImageName]];
        return 1;
    }else if(x > img.x){
        if (self.supportFloat) {
            [img setImage:[UIImage imageNamed:self.halfImageName]];
            return 0.5;
        } else {
            [img setImage:[UIImage imageNamed:self.selectedImageName]];
            return 1;

        }
    }else{
        [img setImage:[UIImage imageNamed:self.normalImageName]];
        return 0;
    }
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
